
class TestHeap {
    public int[] elem;
    public int useSize;
    public TestHeap(int len) {
        this.elem = new int[len];
    }
    public void initElem(int[] arr) {
        for (int i = 0; i < elem.length; i++) {
            elem[i] = arr[i];
            useSize++;
        }
    }
    public void creatBigHeap() {
        for (int parent = (useSize - 1 - 1 )/ 2; parent >= 0; parent--) {
            shifDown(parent, useSize);
        }
    }
    private void shifDown(int parent, int end) {
        int child = 2 * parent + 1;
        while (child < end) {
            if (child + 1 < useSize && elem[child] < elem[child + 1] ) {
                child++;
            }
            if (elem[child] > elem[parent]) {
                swap(parent, child);
                parent = child;
                child = 2 * parent +1;

            } else {
                break;
            }
        }
    }
    public void swap(int parent, int child) {
        int tmp = elem[child];
        elem[child] = elem[parent];
        elem[parent] = tmp;
    }
    public int poll() {
       swap(0,useSize-1);
       useSize--;
       shifDown(0,useSize);
       return elem[0];
    }
}
public class myPriorityqueue {
    public static void main(String[] args) {
        int[] arr = {27,15,19,18,28,34,65,49,25,37};
        TestHeap testHeap = new TestHeap(arr.length);
        testHeap.initElem(arr);
        testHeap.creatBigHeap();
        System.out.println("=====");
        System.out.println(testHeap.poll());
        System.out.println("++++++");
    }
}
